/*
    This file is part of qmessages.
    Copyright (C) 2010  Jason Newton <nevion@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "MessageExtensions.h"

using namespace qmsgio;

int ExampleRequest::fixedSize(){
    return QMessage::fixedSize() + sizeof(length);
}

bool ExampleRequest::parseFixed(QDataStream &stream){
    if(stream.device()->bytesAvailable() >= fixedSize()
            && QMessage::parseFixed(stream)){
        stream>>length;
        request_msg.resize(length);
        return true;
    }else{
        return false;
    }
}

int ExampleRequest::variableSize(){
    return length;
}

bool ExampleRequest::parseVariable(QDataStream &stream){
    if(QMessage::parseVariable(stream)){
        stream.readRawData(request_msg.data(), length);
        return true;
    }else{
        return false;
    }
}

bool ExampleRequest::serialize(QDataStream &stream){
    if(QMessage::serialize(stream)){
        length = request_msg.size();
        stream<<length;
        stream.writeRawData(request_msg.constData(), length);
        return true;
    }else{
        return false;
    }
}


int Reply::fixedSize(){
    return QMessage::fixedSize() + 2 * sizeof(qint8);
}

bool Reply::parseFixed(QDataStream &stream){
    if(stream.device()->bytesAvailable() >= fixedSize()
            && QMessage::parseFixed(stream)){
        qint8 ok[2];
        stream>>ok[0]>>ok[1];
        if(ok[0] == 'o' && ok[1] == 'k'){
            return true;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

bool Reply::serialize(QDataStream &stream){
    if(QMessage::serialize(stream)){
        qint8 ok[2] = {'o', 'k'};
        stream<<ok[0]<<ok[1];
        return true;
    }else{
        return false;
    }
}

int ExampleReply::fixedSize(){
    return Reply::fixedSize() + sizeof(length);
}

int ExampleReply::variableSize(){
    return reply_msg.size();
}

bool ExampleReply::parseFixed(QDataStream &stream){
    if(stream.device()->bytesAvailable() >= fixedSize()
            && Reply::parseFixed(stream)){
        stream>>length;
        reply_msg.resize(length);
        return true;
    }else{
        return false;
    }
}

bool ExampleReply::parseVariable(QDataStream &stream){
    if(Reply::parseVariable(stream)){
        stream.readRawData(reply_msg.data(), length);
        return true;
    }else{
        return false;
    }
}

bool ExampleReply::serialize(QDataStream &stream){
    if(Reply::serialize(stream)){
        length = reply_msg.size();
        stream<<length;
        stream.writeRawData(reply_msg.constData(), length);
        return true;
    }else{
        return false;
    }
}

void registerMessageExtensions(){
    QMessage::register_message<ExampleRequest>(11);
    QMessage::register_message<ExampleReply>(22);
}
